package com.niuwa.p2p.action.loanclaim;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.action.loanclaim.model.LoanClaimWithProdModel;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.service.claimtransfer.ILoanClaimService;
import com.niuwa.p2p.service.messageTemplate.IMessageTemplateService;
import com.niuwa.p2p.vo.claim.LoanClaimVo;

/**
 * 
 * @author: futianliang
 * 
 */
public class LoanClaimWithProdAction extends JsonAction {
	private static final long serialVersionUID = -8900124777631535966L;
	/** 日志 */
	private static Logger logger = Logger
			.getLogger(LoanClaimWithProdAction.class);
	@Autowired
	private ILoanClaimService loanClaimService;
	@Autowired
	private IMessageTemplateService templateService;

	/* *
	 * 跳转到债权列表
	 */
	@Action(value = "loanClaimManage")
	public String gotoLoanClaimListManagePage() {
		return "loanClaimManage";
	}

	/**
	 * 查询牛娃账户列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "loanClaimList")
	public String loanClaimList() throws Exception {
		setObjectReSult(queryPage());
		logger.info("claimtransferList action execute success");
		//Map<String,String> map = templateService.fetchMessageTemplateContentByType(null, null, null);
		return NONE;
	}

	/**
	 * 分页查询
	 * 
	 * @return Pagination<NwAccount>
	 */
	private PageBean<LoanClaimVo> queryPage() {

		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		if(paramMap.get("productNoAndName")!=null){
			paramMap.put("productNoAndName", String.valueOf(paramMap.get("productNoAndName")).trim().replaceAll("%", "\\\\%").replaceAll("_", "\\\\_"));
		}

		// 查选分页集合
		// PageBean<LoanClaimVo> page =
		// loanClaimService.getLoanClainList(paramMap);
		// 总条数
		int totalCount = loanClaimService.getLoanClainListCount(paramMap);
		PageBean<LoanClaimVo> page = new PageBean<LoanClaimVo>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		List<LoanClaimVo> entites = loanClaimService.getLoanClainList(paramMap);
		List<LoanClaimWithProdModel> models = new ArrayList<LoanClaimWithProdModel>();
		if (!CollectionUtils.isEmpty(entites)) {
			for (LoanClaimVo vo : entites) {
				models.add(new LoanClaimWithProdModel(vo));
			}
		}
		page.setRows(models);
		return page;
	}
}
